<?php

namespace IDock\Insurance\CHAC\Response;

use IDock\Http\Response;
use IDock\Insurance\CHAC\CHACEnums;
use IDock\Printer\VehicleModelPrinter;
use IDock\Printer\VehiclePrinter;

class BVehicleModelResponse extends AllCHACResponse
{

    /*** @var VehicleModelPrinter $printer */
    public $printer = VehicleModelPrinter::class;

    public $urlDecode = true;

    public function printerInit(): Response
    {
        $body = $this->getBody();
        if ($body == '未查询到车型信息') {
            $this->setError($body);
            return $this;
        }

        $list = $this->get('vehicleList','');


        if ($list) {
            $list = json_decode($list,true);
        }

        foreach ($list as $datum) {
            $vehiclePrinter = new VehiclePrinter();
            $vehiclePrinter->vehicleModelCode = $datum['modelCode'] ?? '';
            $vehiclePrinter->vehicleName = $datum['modelName'] ?? '';
            $vehiclePrinter->vehicleTonnage = $datum['tonnage'] ?? '';
            $vehiclePrinter->vehicleExhaust = $datum['vehicleDisplacement'] ?? '';
            $vehiclePrinter->vehiclePrice = $datum['vehiclePrice'] ?? '';
            $vehiclePrinter->vehiclePriceWithOutTax = $datum['taxVehiclePrice'] ?? '';
            $vehiclePrinter->remark = $datum['remark'] ?? '';
            $vehiclePrinter->brandName = $datum['vehicleBrand'] ?? '';

            $vehiclePrinter->energyTypeCode = CHACEnums::$energyType1[$datum['energyType']];

            $vehiclePrinter->isNewEnergy = ($datum['energyFlag'] ?? '0') == '1' ? '1' : '0';

            $vehiclePrinter->item['configModel'] = $datum['configModel'] ?? '';

            $vehiclePrinter->vehicleSeat = $datum['seat'] ?? '';

            if ($vehiclePrinter->vehicleExhaust) {
                $vehiclePrinter->vehicleExhaust = (string)($vehiclePrinter->vehicleExhaust / 1000);
            }

            if ($datum['taxCutFlag'] ?? '') {
                $vehiclePrinter->taxReduceCode = CHACEnums::$taxReduceCode[$datum['taxCutFlag']];
            }

            if ($datum['taxCutProp'] ?? '') {
                $vehiclePrinter->taxReduceCode = 'TR4';
            }

            $this->printer->setModelList($vehiclePrinter);
        }

        return $this;
    }
}
